package com.castle.fortress.admin.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsArticleTagsDto;
import com.castle.fortress.admin.cms.entity.CmsArticleTagsEntity;
import com.castle.fortress.admin.cms.entity.CmsCustTagEntity;
import com.castle.fortress.admin.cms.mapper.CmsArticleTagsMapper;
import com.castle.fortress.admin.cms.service.CmsArticleTagsService;
import com.castle.fortress.admin.cms.service.CmsCustTagService;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * cms内容标签管理表 服务实现类
 *
 * @author castle
 * @since 2022-03-18
 */
@Service
public class CmsArticleTagsServiceImpl extends ServiceImpl<CmsArticleTagsMapper, CmsArticleTagsEntity> implements CmsArticleTagsService {
    @Autowired
    private CmsCustTagService cmsCustTagService;
    /**
    * 获取查询条件
    * @param cmsArticleTagsDto
    * @return
    */
    public QueryWrapper<CmsArticleTagsEntity> getWrapper(CmsArticleTagsDto cmsArticleTagsDto){
        QueryWrapper<CmsArticleTagsEntity> wrapper= new QueryWrapper();
        if(cmsArticleTagsDto != null){
            CmsArticleTagsEntity cmsArticleTagsEntity = ConvertUtil.transformObj(cmsArticleTagsDto,CmsArticleTagsEntity.class);
            wrapper.eq(cmsArticleTagsEntity.getId() != null,"id",cmsArticleTagsEntity.getId());
            wrapper.eq(cmsArticleTagsEntity.getArticleId() != null,"article_id",cmsArticleTagsEntity.getArticleId());
            wrapper.eq(cmsArticleTagsEntity.getTagId() != null,"tag_id",cmsArticleTagsEntity.getTagId());
            wrapper.eq(StrUtil.isNotEmpty(cmsArticleTagsEntity.getTagType()),"tag_type",cmsArticleTagsEntity.getTagType());
        }
        return wrapper;
    }


    @Override
    public IPage<CmsArticleTagsDto> pageCmsArticleTags(Page<CmsArticleTagsDto> page, CmsArticleTagsDto cmsArticleTagsDto) {
        QueryWrapper<CmsArticleTagsEntity> wrapper = getWrapper(cmsArticleTagsDto);
        Page<CmsArticleTagsEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsArticleTagsEntity> cmsArticleTagsPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsArticleTagsDto> pageDto = new Page(cmsArticleTagsPage.getCurrent(), cmsArticleTagsPage.getSize(),cmsArticleTagsPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsArticleTagsPage.getRecords(),CmsArticleTagsDto.class));
        return pageDto;
    }


    @Override
    public List<CmsArticleTagsDto> listCmsArticleTags(CmsArticleTagsDto cmsArticleTagsDto){
        QueryWrapper<CmsArticleTagsEntity> wrapper = getWrapper(cmsArticleTagsDto);
        List<CmsArticleTagsEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsArticleTagsDto.class);
    }

    @Async
    @Override
    public void updateAttrByArticle(Long articleId, List<Long> attrs) {
        QueryWrapper<CmsArticleTagsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id",articleId);
        wrapper.eq("tag_type","00");
        baseMapper.delete(wrapper);
        if(attrs!=null && !attrs.isEmpty()){
            List<CmsArticleTagsEntity> list = new ArrayList<>();
            for(Long attr:attrs){
                CmsArticleTagsEntity entity = new CmsArticleTagsEntity();
                entity.setArticleId(articleId);
                entity.setTagId(attr);
                entity.setTagType("00");
                list.add(entity);
            }
            saveBatch(list);
        }
    }

    @Async
    @Override
    public void updateTagsByArticle(Long articleId, List<Object> tags,String type) {
        QueryWrapper<CmsArticleTagsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id",articleId);
        wrapper.eq("tag_type","01");
        baseMapper.delete(wrapper);
        if(tags!=null && !tags.isEmpty()){
            List<CmsCustTagEntity> allTags = cmsCustTagService.list();
            List<Long> t= new ArrayList<>();
            List<CmsCustTagEntity> insertTags = new ArrayList<>();
            List<CmsCustTagEntity> updateTags = new ArrayList<>();
            for(Object o:tags){
                boolean isFind = false;
                for(CmsCustTagEntity a:allTags){
                    if(o.toString().equals(a.getId().toString())){
                        t.add(a.getId());
                        isFind = true;
                        a.setWeight(a.getWeight()==null?1:a.getWeight()+1);
                        updateTags.add(a);
                        break;
                    }
                }
                if(!isFind){
                    CmsCustTagEntity tagEntity = new CmsCustTagEntity();
                    tagEntity.setTitle(o.toString());
                    tagEntity.setCreateTime(new Date());
                    tagEntity.setWeight(1);
                    insertTags.add(tagEntity);
                }
            }
            if(!insertTags.isEmpty()){
                cmsCustTagService.saveBatch(insertTags);
                for(CmsCustTagEntity e:insertTags){
                    t.add(e.getId());
                }
            }
            if("00".equals(type)){
                if(!updateTags.isEmpty()){
                    cmsCustTagService.updateBatchById(updateTags);
                }
            }
            if(!t.isEmpty()){
                List<CmsArticleTagsEntity> list = new ArrayList<>();
                for(Long attr:t){
                    CmsArticleTagsEntity entity = new CmsArticleTagsEntity();
                    entity.setArticleId(articleId);
                    entity.setTagId(attr);
                    entity.setTagType("01");
                    list.add(entity);
                }
                saveBatch(list);
            }
        }
    }
}

